package com.example.helloandroid;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.NoSuchElementException;
import java.util.Scanner;

import android.app.Activity;
import android.util.Log;


public class TempTest {
    private static final String TAG = "TempTest";
    private Activity mActivity;

    private void init(Activity activity) {
        mActivity = activity;
    }

    private void dummyCall() {
    }

    private static final String S_PATH_PROC_LOADAVG = "/proc/loadavg";
    private float getCpuLoad() {
        float cpuLoad = 0f;

        // Read "/proc/loadavg". Parse the line with following format:
        // "0.01 0.17 0.58 1/788 4623"
        Scanner s = null;
        try {
            s = new Scanner( new BufferedReader( new FileReader( S_PATH_PROC_LOADAVG ) ) );
            float last1Min = Float.parseFloat( s.next() );
            float last5Min = Float.parseFloat( s.next() );
            float last15Min = Float.parseFloat( s.next() );
            Log.d( TAG, "loadavg = (" + last1Min + "/" + last5Min + "/" + last15Min + ")" );

            cpuLoad = last1Min;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchElementException e) {
            e.printStackTrace();
        } finally {
            if ( s != null ) {
                s.close();
            }
        }

        return cpuLoad;
    }

    @SuppressWarnings("unused")
    void doTest(Activity activity) {
        init(activity);

        float t = getCpuLoad();
        dummyCall();
    }
}
